<template>
  <my-container v-loading="pageLoading">
    <!--查询-->
    <template #header>
      <el-form class="ad-form-query" :inline="true" :model="filter" @submit.native.prevent>
        <el-form-item>
          <el-input
            v-model="filter.name"
            placeholder="名称"
            clearable
            @keyup.enter.native="onSearch"
          >
            <template #prefix>
              <i class="el-input__icon el-icon-search" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" icon="el-icon-search" @click="onSearch">查询</el-button>
        </el-form-item>
        <el-form-item v-if="checkPermission(['admin_sysjobgroup_add'])">
          <el-button type="primary" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </el-form-item>
        <el-form-item v-if="checkPermission(['admin_sysjobgroup_delete'])">
          <my-confirm-button
            :disabled="sels.length === 0"
            :type="'delete'"
            :placement="'bottom-end'"
            style="margin-left: 0px;"
            @click="onBatchDelete"
          >
            <template #content>
              <p>确定要批量删除吗？</p>
            </template>
            批量删除
          </my-confirm-button>
        </el-form-item>
      </el-form>
    </template>

    <!--列表-->
    <el-table
      v-loading="listLoading"
      :data="groups"
      border
      highlight-current-row
      :header-cell-style="{background:'#f9f9f9'}"
      @selection-change="selsChange"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column type="index" width="80" label="#" />
      <el-table-column prop="name" label="分组名" width />
      <el-table-column prop="code" label="编码" width />
      <el-table-column prop="createTime" label="创建时间" :formatter="formatCreatedTime" width />
      <el-table-column label="操作" width="180">
        <template #default="{ $index, row }">
          <my-confirm-button
            v-if="checkPermission(['admin_sysjobgroup_delete'])"
            type="delete"
            icon="el-icon-delete"
            :loading="row._loading"
            :validate-data="row"
            @click="onDelete($index, row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <my-pagination
      ref="pager"
      :total="total"
      @get-page="getJobGroupList"
    />

    <!--新增窗口-->
    <el-dialog
      title="新增分组"
      :visible.sync="addFormVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <el-form ref="addForm" :model="addForm" label-width="80px" :rules="addFormRules" size="medium">
        <el-row>
          <el-col>
            <el-form-item label="分组名" prop="name">
              <el-input v-model="addForm.name" auto-complete="off" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="编码" prop="code">
              <el-input v-model="addForm.code" auto-complete="off" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <my-confirm-button type="submit" :validate="addFormValidate" :loading="addLoading" @click="onAddSubmit" />
        </div>
      </template>

    </el-dialog>
  </my-container>
</template>

<script>
import { formatTime } from '@/utils'
import { getJobGroupList, removeJobGroup, addJobGroup } from '@/api/admin/jobgroup'
import MyContainer from '@/components/my-container'
import MyConfirmButton from '@/components/my-confirm-button'

export default {
  name: 'JobGroup',
  components: { MyContainer, MyConfirmButton },
  data() {
    return {
      filter: {
        name: ''
      },
      groups: [],
      total: 0,
      sels: [], // 列表选中列
      listLoading: false,

      pageLoading: false,
      addDialogFormVisible: false,

      addFormVisible: false, // 新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: '请输入分组名', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }]
      },
      // 新增界面数据
      addForm: {
        name: '',
        code: ''
      },
      addFormRef: null,
      deleteLoading: false
    }
  },
  mounted() {
    this.getJobGroupList()
  },
  beforeUpdate() {
  },
  methods: {

    formatCreatedTime: function(row, column, time) {
      return formatTime(time, 'YYYY-MM-DD HH:mm')
    },
    onSearch() {
      this.$refs.pager.setPage(1)
      this.getJobGroupList()
    },
    // 获取分组列表
    async getJobGroupList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager,
        filter: this.filter
      }
      this.listLoading = true
      const res = await getJobGroupList(params)
      this.listLoading = false
      if (!res?.success) {
        return
      }
      this.total = res.data.totalCount
      const data = res.data.rows
      data.forEach(d => {
        d._loading = false
      })
      this.groups = data
    },

    // 显示新增界面
    onAdd() {
      this.addFormVisible = true
    },
    // 新增验证
    addFormValidate: function() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 新增
    async onAddSubmit() {
      this.addLoading = true
      const para = _.cloneDeep(this.addForm)

      const res = await addJobGroup(para)
      this.addLoading = false

      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.addOk'),
        type: 'success'
      })
      this.$refs['addForm'].resetFields()
      this.addFormVisible = false
      this.getJobGroupList()
    },

    closeAddForm() {
      this.$refs.addForm.resetFields()
    },

    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = []
      para.push(row.id)
      const res = await removeJobGroup(para)
      row._loading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })

      this.getJobGroupList()
    },
    // 批量删除
    async onBatchDelete() {
      const para = { ids: [] }
      para.ids = this.sels.map(s => {
        return s.id
      })
      this.deleteLoading = true
      const res = await removeJobGroup(para.ids)
      this.deleteLoading = false

      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.batchDeleteOk'),
        type: 'success'
      })

      this.getJobGroupList()
    },
    selsChange: function(sels) {
      this.sels = sels
    }
  }
}
</script>
